package org.activiti;

//import javax.jms.ConnectionFactory;
import javax.sql.DataSource;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.RuntimeServiceImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MyApplication {

    @Autowired
    private ApplicationContext appContext;

    /*@Autowired
     private ActiveMQConnectionFactoryProperties config;*/
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    /*@Bean
     ConnectionFactory jmsConnectionFactory() {
     return new ActiveMQConnectionFactory(this.config.getBrokerURL());
     }
     */
    @Bean(name = "camelContext")
    public CamelContext camel() throws Exception {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //Context.setCommandContext(new CommandContext(null, null));
        SimpleRegistry registry = new SimpleRegistry();
        //registry.put("runtimeService", new RuntimeServiceImpl()); 
        registry.put("runtimeService", runtimeService); 
        CamelContext camelContext = new DefaultCamelContext(registry);
        camelContext.addComponent("activemq", ActiveMQComponent.activeMQComponent("tcp://localhost:61616"));
        camelContext.addRoutes(new AsyncCamelRoute());
        camelContext.start();
        return camelContext;
    }

    @Bean
    public CommandLineRunner init(final MyService myService) {

        return new CommandLineRunner() {
            public void run(String... strings) throws Exception {
                myService.createDemoUsers();
            }
        };

    }

    //local
    /*@Bean
    public DataSource database() {
        return DataSourceBuilder.create()
                .url("jdbc:postgresql://localhost:5432/activiti")
                .username("postgres")
                .password("root")
                .driverClassName("org.postgresql.Driver")
                .build();
    }*/
    
    //server
    @Bean
    public DataSource database() {
        return DataSourceBuilder.create()
                .url("jdbc:postgresql://localhost:5452/activiti")
                .username("postgres")
                .password("postgres")
                .driverClassName("org.postgresql.Driver")
                .build();
    }

    /*@ConfigurationProperties()
     public static class ActiveMQConnectionFactoryProperties {

     private String brokerURL = "tcp://localhost:61616";

     private boolean inMemory = false;

     private boolean pooled = false;

     // Will override brokerURL if inMemory is set to true
     public String getBrokerURL() {
     /*if (this.inMemory) {
     return "vm://localhost";
     } else {
     return this.brokerURL;
     //}
     }
     }*/
}
